/**
 * @author Pieter Michels / wellconsidered
 * pieter@wellconsidered.be
 *
 * Open source under the GNU Lesser General Public License (http://www.opensource.org/licenses/lgpl-license.php)
 * Copyright © 2009 Pieter Michels / wellconsidered
 * 
 * This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License 
 * as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty 
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public License along with this library; 
 * if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
 */
 
package be.wellconsidered.apis.lighthouse.resources
{
	import be.wellconsidered.apis.lighthouse.LightHouse;
	import be.wellconsidered.apis.lighthouse.connectors.*;
	import be.wellconsidered.apis.lighthouse.connectors.events.*;
	import be.wellconsidered.logging.Logger;
	
	public class TicketBins extends AbstractResource
	{		
		private var _xmlNewTicketBin:XML;
		
		public function TicketBins(lighthouseReference:LightHouse)
		{
			super(lighthouseReference);
		}
		
		/**
		 * List TicketBins
		 */
		public function listTicketBins(projectID:int):void
		{
			Logger.log("List TicketBins");
			
			var connGET:GET = new GET(_lhRef.getURL() + "projects/" + projectID + "/bins.xml", _lhRef.getToken());
			
			connGET.addEventListener(GETEvents.COMPLETE, onTicketBinListingComplete, false, 0, true);
			
			function onTicketBinListingComplete(e:GETEvents):void
			{
				dispatchEvent(new TicketBinEvents(TicketBinEvents.LIST_COMPLETE, false, false, new XML(e.data)));	
			}
		}
		
		/**
		 * Get TicketBin
		 */
		public function getTicketBin(projectID:int, ticketBinID:int):void
		{
			Logger.log("Get TicketBin (" + ticketBinID + ")");
			
			var connGET:GET = new GET(_lhRef.getURL() + "projects/" + projectID + "/bins/" + ticketBinID + ".xml", _lhRef.getToken());
			
			connGET.addEventListener(GETEvents.COMPLETE, onTicketBinFetchComplete, false, 0, true);
			
			function onTicketBinFetchComplete(e:GETEvents):void
			{
				dispatchEvent(new TicketBinEvents(TicketBinEvents.GET_COMPLETE, false, false, new XML(e.data)));	
			}
		}
		
		/**
		 * Create TicketBin
		 */
		public function createTicketBin(projectID:Number, ticketBinName:String, ticketBinQuery:String, ticketBinDefault:Boolean = false):void
		{
			Logger.log("Create TicketBin");
			
			var xmlTicketBin:XML = 	<ticket-bin>
								  		<name>{ticketBinName}</name>
								  		<query>{ticketBinQuery}</query>
								  		<default type="boolean">{ticketBinDefault}</default>
									</ticket-bin>
					
			var connPOST:POST = new POST(_lhRef.getURL() + "projects/" + projectID + "/bins.xml", _lhRef.getToken(), _lhRef.getBase64Encoding(), xmlTicketBin);
			
			connPOST.addEventListener(POSTEvents.COMPLETE, onCreateTicketBinComplete, false, 0, true);
			
			function onCreateTicketBinComplete(e:POSTEvents):void
			{				
				dispatchEvent(new TicketBinEvents(TicketBinEvents.CREATE_COMPLETE, false, false, new XML(e.data)));	
			}
		}
		
		/**
		 * Update TicketBin
		 */
		public function updateTicketBin(projectID:int, ticketBinXML:XML):void
		{
			Logger.log("Update TicketBin (" + ticketBinXML.id + ")");
			
			var connPUT:PUT = new PUT(_lhRef.getURL() + "projects/" + projectID + "/bins/" + ticketBinXML.id + ".xml", _lhRef.getToken(), _lhRef.getBase64Encoding(), ticketBinXML);
			
			connPUT.addEventListener(PUTEvents.COMPLETE, onUpdateTicketBinComplete, false, 0, true);
			
			function onUpdateTicketBinComplete(e:PUTEvents):void
			{				
				dispatchEvent(new TicketBinEvents(TicketBinEvents.UPDATE_COMPLETE, false, false, new XML(e.data)));	
			}
		}
		
		/**
		 * Delete TicketBin
		 */
		public function deleteTicketBin(projectID:int, ticketBinID:int):void
		{
			Logger.log("Delete TicketBin (" + ticketBinID + ")");
			
			var connDELETE:DELETE = new DELETE(_lhRef.getURL() + "projects/" + projectID + "/bins/" + ticketBinID + ".xml", _lhRef.getToken(), _lhRef.getBase64Encoding());
			
			connDELETE.addEventListener(DELETEEvents.COMPLETE, onDeleteTicketBinComplete, false, 0, true);
			
			function onDeleteTicketBinComplete(e:DELETEEvents):void
			{				
				dispatchEvent(new TicketBinEvents(TicketBinEvents.DELETE_COMPLETE, false, false, e.data));	
			}
		}
	}
}